<!DOCTYPE html>
<html>
<head>
    <include file="Include:meta"/>
    <title>添加发票-目的地运营集团数据管理平台</title>
    <include file="Include:style"/>
    <style>
        .w50 {
            width: 50px;
        }
    </style>
</head>
<body>
<include file="Include:header"/>
<include file="Include:left"/>
<div id="main">
    <div class="crumbs">
        <div class="crumbs-text">发票列表 > 添加发票</div>
    </div>
    <div id="content">
        <form action="__APP__/ContractBack/add_invoice_head" method="post" onsubmit="return verify()">
            <div class="form-main">
                <div class="form-title">添加发票</div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>发票编号</div>
                    <div class="input-item">
                        <input type="text" class="form-control w200" name="number" value="{$number}" autocomplete="off"
                               required readonly>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>关联项目</div>
                    <div class="input-item">
                        <select name="project_id" id="project_id" class="form-control w500 required_select"
                                autocomplete="off"
                                required>
                            <option value="-10">请选择</option>
                            <foreach name="res_crm_project" item="vo">
                                <option value="{$vo['project_id']}">{$vo['project_num']}--{$vo['name']}</option>
                            </foreach>
                        </select>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>关联合同</div>
                    <div class="input-item">
                        <select name="contract_id" id="contract_id" class="form-control w500 required_select"
                                autocomplete="off"
                                required>
                            <option value="-10">请先选关联合同</option>
                            <!--<option value="-10">请选择</option>
                            <foreach name="res_contract" item="vo">
                                <option value="{$vo['contract_id']}">{$vo['file_name']}-{$vo['name']}</option>
                            </foreach>-->
                        </select>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>发票号码</div>
                    <div class="input-item">
                        <textarea class="form-control w200" id="no" name="no"></textarea>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>发票抬头</div>
                    <div class="input-item">
                        <input type="text" class="form-control w200" name="title">
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>关联收款</div>
                    <div class="input-item">
                        <select name="back_id" id="back_id" class="form-control w200 required_select" autocomplete="off"
                                required>
                            <option value="-10">请先选关联收款</option>
                        </select>
                        <br>
                        <span id="back"></span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>开票金额</div>
                    <div class="input-item" id="make_money">

                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>开票日期</div>
                    <div class="input-item">
                        <input type="text" name="make_date" value="{$today}"
                               class="form-control Wdate w200 required_text"
                               onClick="WdatePicker()" readonly autocomplete="off">
                        <span class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>申请人</div>
                    <div class="input-item">
                        <select name="proposer" class="form-control w200 required_select" autocomplete="off"
                                required>
                            <option value="{:$_SESSION['dms_uid']}">{:$_SESSION['dms_username']}</option>
                            <foreach name="user_list" item="vo">
                                <option value="{$vo.id}">{$vo.username}</option>
                            </foreach>
                        </select>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label">备注</div>
                    <div class="input-item">
                        <textarea name="remark" class="form-control w400" autocomplete="off"></textarea>
                    </div>
                </div>
            </div>
            <div class="form-btn-item" id="submit"><input type="submit" value="提交" class="btn btn-submit">
                <button class="btn btn-default goback" type="button">返回</button>
            </div>
        </form>
    </div>
</div>
</body>
<include file="Include:javascript"/>
<script type="text/javascript">
    $(function () {
        /*项目联动合同*/
        $("[name='project_id']").change(function () {
            $('#submit').show();
            var val = $(this).val();
            if (val == -10) {
                $('#contract_id,#make_money,#back_id,#back').html('');
            } else {
                $.ajax({
                    type: "GET",
                    async: false,
                    dataType: "json",
                    data: {"project_id": val},
                    url: "__APP__/ContractBack/getContract",
                    success: function (data) {
                        if (data) {
                            var html = "<option value='-10'>请选择关联合同</option>";
                            for (var i = 0; i < data.length; i++) {
                                html += "<option value=" + data[i].contract_id + ">" + data[i].number + "--" + data[i].file_name + "</option>"
                            }
                            $("[name='contract_id']").html(html).select2();
                        } else {
                            defeated_alert('没有关联合同信息')
                        }
                    }
                })
            }
        })
        /*合同联动收款*/
        $("[name='contract_id']").change(function () {
            $('#submit').show();
            var val = $(this).val();
            if (val == -10) {
                $('#make_money,#back_id,#back').html('');
            } else {
                $.ajax({
                    type: "GET",
                    async: false,
                    dataType: "json",
                    data: {"contract_id": val},
                    url: "__APP__/ContractBack/getContractBack",
                    success: function (data) {
                        if (data) {
                            var html = "<option value='-10'>请选择关联收款</option>";
                            for (var i = 0; i < data.length; i++) {
                                html += "<option value=" + data[i].back_id + ">第" + data[i].stage + "阶段</option>"
                            }
                            $("[name='back_id']").html(html).select2();
                        } else {
                            defeated_alert('没有关联收款信息')
                        }
                    }
                })
            }
        })

        /*收款计划联动开票金额*/
        $("[name='back_id']").change(function () {
            $('#submit').show();
            $('#make_money,#back').html('');
            var val = $(this).val();
            if (val == -10) {
                $('#make_money,#back').html('');
            } else {
                $.ajax({
                    type: "GET",
                    async: false,
                    dataType: "json",
                    data: {"back_id": val},
                    url: "__APP__/ContractBack/getContractInvoiceMakeMoney",
                    success: function (data) {
                        $("#back").append("该阶段总金额：" + data.stage_money + "&nbsp;&nbsp;已开票：" + data.sum_make_money + "&nbsp;&nbsp;未开票：" + data.money);
                        $("#make_money").append(" <input type='number' class='form-control w200' name='make_money' max='" + data.money + "' value=" + data.money + ">");
                        if (data.money == 0) {
                            defeated_alert('此阶段发票已开完');
                            $('#submit').hide();
                        }
                    }
                })
            }
        })
    });

    function verify() {
        var project_id = $("#project_id").val();
        var contract_id = $("#contract_id").val();
        var no = $("#no").val();
        var title = $("input[name='title']").val();
        var back_id = $("#back_id").val();
        var make_money = $("input[name='make_money']").val()
        if (project_id < 0) {
            defeated_alert('请选择关联项目');
            return false;
        }
        if (contract_id < 0) {
            defeated_alert('请选择关联合同');
            return false;
        }
        if (no.length == 0) {
            defeated_alert('请填写发票号码');
            return false;
        }
        if (title.length == 0) {
            defeated_alert('请填写发票抬头');
            return false;
        }
        if (back_id < 0) {
            defeated_alert('请选择关联收款');
            return false;
        }
        if (make_money == null || make_money == 0 || make_money == "") {
            defeated_alert('请填写收款金额');
            return false;
        }
    }
</script>
</html>